﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace LINQToSQL_app1
{
    class Program
    {
        static string conn = @"Server=THINKPAD\CPPER;Database=DbTest;uid = sa;password=123456";
        static void Main(string[] args)
        {
            //Delete();
            Update();
        }

        static void Select()
        {
            DbTestDataContext dc = new DbTestDataContext();
            var query = dc.Student;
            foreach (Student item in query)
            {
                Console.WriteLine("{0} | {1} | {2}", item.ID, item.Name, item.Age);
            }
        }

        static void SelectBase()
        {
            //using System.Data.Linq;
            DataContext dc = new DataContext(conn);
            IEnumerable<Student> myStudent = dc.ExecuteQuery<Student>("SELECT * FROM Student", "");
            foreach(Student item in myStudent)
            {
                 Console.WriteLine("{0} | {1} | {2}",item.ID,item.Name,item.Age);
            }
        }

        static void Insert()
        {
            DbTestDataContext dc = new DbTestDataContext();
            Student i = new Student();
            i.Name = "hank";
            i.Age = 28;//不需要设置自增字段
            dc.Student.InsertOnSubmit(i);
            dc.SubmitChanges();

            var query = dc.Student;//立即可见新记录
            foreach (Student item in query)
            {
                Console.WriteLine("{0} | {1} | {2}", item.ID, item.Name, item.Age);
            }
        }

        static void InsertBase()
        {
            //using System.Data.Linq;
            DataContext dc = new DataContext(conn);
            Student i = new Student();
            i.Name = "alan";
            i.Age = 28;

            Table<Student> myStudent = dc.GetTable<Student>();
            myStudent.InsertOnSubmit(i);
            dc.SubmitChanges();
        }

        static void Delete()
        { 
            DbTestDataContext dc = new DbTestDataContext();
            Table<Student> s = dc.Student;//.Where(r => r.ID == 1);
            IEnumerable<Student>  i = s.Where<Student>(r => r.ID==1);
            List<Student> z = i.ToList<Student>();
            dc.Student.DeleteOnSubmit(z[0]);
            dc.SubmitChanges();
        }

        static void Update()
        {
            DbTestDataContext dc = new DbTestDataContext();
            Table<Student> s = dc.Student;//.Where(r => r.ID == 1);
            //IEnumerable<Student> i = s.Where<Student>(r => r.ID == 1);
            List<Student> z = s.ToList<Student>();
            z[0].Age = 100;
            dc.SubmitChanges();   
        }


    }
}
